package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private ApartmentFacilityMapper apartmentFacilityMapper;

    @Autowired
    private ApartmentLabelMapper apartmentLabelMapper;
    @Autowired
    private ApartmentFeeValueMapper apartmentFeeValueMapper;

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService  apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;

    @Autowired
    private RoomInfoMapper  roomInfoMapper;

    @Autowired
    private ProvinceInfoMapper provinceInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;
    @Autowired
    private DistrictInfoMapper districtInfoMapper;
    @Override
    public IPage<ApartmentItemVo> getApartmentItem(Page<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.getApartmentItem(page, queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentItemDetailById(Long id) {
        ApartmentInfo byId = this.getById(id);
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(byId, apartmentDetailVo);

        List<GraphVo> graphVoList = graphInfoMapper.getGraphInfoMapperByApartmentId(id);
        List<LabelInfo> labelInfoList = labelInfoMapper.getLabelInfoMapperByApartmentId(id);
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.getFacilityInfoMapperByApartmentId(id);
        List<FeeValueVo> feeValueVoList = feeValueMapper.getFeeValueMapperByApartmentId(id);

        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);


        return apartmentDetailVo;
    }

    @Override
    public void saveOrUpdateApartmentSubmitVo(ApartmentSubmitVo apartmentSubmitVo) {
        Long id = apartmentSubmitVo.getId();

        String provinceName = provinceInfoMapper.selectById(apartmentSubmitVo.getProvinceId()).getName();
        apartmentSubmitVo.setProvinceName(provinceName);

        String cityName = cityInfoMapper.selectById(apartmentSubmitVo.getCityId()).getName();
        apartmentSubmitVo.setCityName(cityName);

        String districtName = districtInfoMapper.selectById(apartmentSubmitVo.getDistrictId()).getName();
        apartmentSubmitVo.setDistrictName(districtName);


        this.saveOrUpdate(apartmentSubmitVo);

        //把公寓信息的数据添加成功，其余的数据下面一一处理，因为不在一张表
        //mybatis-plus的saveOrUpdate方法，如果是添加那么会自动帮你获取自增主键 id
        //上面我们获取的id    有可能是空的
        //添加完成后 apartmentSubmitVo.getId()  必然不为空

        if(id != null){
//            改  先把这个对象当中剩余的4个数据全删一遍
//            删除的是公寓和数据的中间表信息
            apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                    .eq(ApartmentFacility::getApartmentId,id));
            apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                    .eq(ApartmentLabel::getApartmentId,id));
            apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>()
                    .eq(ApartmentFeeValue::getApartmentId,id));
            graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id)
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT));
        }

//        无论是增还是改，下面这4个数据都要重新添加一遍

//        @Schema(description="公寓配套id")
//        private List<Long> facilityInfoIds;
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        ArrayList<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        facilityInfoIds.forEach(facilityInfoId -> {
            ApartmentFacility apartmentFacility = ApartmentFacility.builder().apartmentId(apartmentSubmitVo.getId()).facilityId(facilityInfoId).build();
            apartmentFacilities.add(apartmentFacility);
        });
        apartmentFacilityService.saveBatch(apartmentFacilities);


//
//        @Schema(description="公寓标签id")
//        private List<Long> labelIds;
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
        labelIds.forEach(labelId -> {
            ApartmentLabel apartmentLabel = ApartmentLabel.builder().apartmentId(apartmentSubmitVo.getId()).labelId(labelId).build();
            apartmentLabels.add(apartmentLabel);
        });
        apartmentLabelService.saveBatch(apartmentLabels);


//
//        @Schema(description="公寓杂费值id")
//        private List<Long> feeValueIds;
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        ArrayList<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        feeValueIds.forEach(feeValueId -> {
            ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                    .apartmentId(apartmentSubmitVo.getId())
                    .feeValueId(feeValueId)
                    .build();
            apartmentFeeValues.add(apartmentFeeValue);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValues);


//
//        @Schema(description="公寓图片id")
//        private List<GraphVo> graphVoList;
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        ArrayList<GraphInfo> graphInfos = new ArrayList<>();
        graphVoList.forEach(graphVo -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemId(apartmentSubmitVo.getId());
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfos.add(graphInfo);
        });
        graphInfoService.saveBatch(graphInfos);

    }

    @Override
    public void removeApartmentById(Long id) {
        Long count = roomInfoMapper.selectCount(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId,id));


        this.removeById(id);
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>().eq(ApartmentFacility::getApartmentId,id));
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId,id));
        apartmentFeeValueService.remove(new LambdaQueryWrapper<ApartmentFeeValue>().eq(ApartmentFeeValue::getApartmentId,id));
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType, ItemType.APARTMENT));

    }


}




